package lab03.test03;

import java.io.*;

public class Test03 {
    public static void main(String[] args) {
        String path = "F:\\test.txt";
        FileOutputStream fileWriter = null;//
        FileInputStream fileReader = null;//初始化
        String s = " ";


        try {
            fileWriter = new FileOutputStream(path);//建立一个新的录入路径
            fileWriter.write(new String("Beijing欢迎你！").getBytes());//写入字符串并转换成字节
        } catch (Exception e) {
            e.printStackTrace();
        }

        //读取字节并转换成字符串
        try {
            fileReader = new FileInputStream(path);
            byte[] b = new byte[fileReader.available()];//字节转换成数组
            while (fileReader.read(b) != -1) {//将字节转换成字符串
                s = new String(b);
                System.out.println(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //增加
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(path));//建立一个新的录入路径
            BufferedReader reader = new BufferedReader(new FileReader(path));

            writer.write(s);
            writer.newLine();


            writer.write("我的学号是：你自己的学号");


            writer.close();//关闭文件保存并退出

            while (true) {
                String line = reader.readLine();
                if (line != null) {
                    System.out.println(line);
                } else
                    break;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

